package zz.extjs.servlet;
/**
 * 对于用户登录时选择保存2周登录的
 * 用cookie保存用户名密码为“userinfo”（密码经过算子参与的md5加密）
 * 同时保存md5算子在cookie“loginoper”中，取出用户名密码，
 * 设置校验码和md5算子2个session，提交给checkMember，从而实现自动登录
 */
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zz.extjs.factory.DAOFactory;

public class CheckCookie extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }        
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String username=null;
        String password=null;
        String md5oper=null;
        //取得Cookie的全部属性
        Cookie c[]=request.getCookies();
        int i;
        for(i=0;i<c.length;i++) {
            
            if("userinfo".equals(c[i].getName())) {
                
                //Cookie中有用户名密码，保存,结构：用户名+md5算子+密码,暂时保存在username中
                username=c[i].getValue();
                 
                //password=c[i].getValue();
           }
            if("loginoper".equals(c[i].getName())) {	//设置md5算子，由cookie保存上次登录成功是的
            	md5oper = c[i].getValue();
            }
        }
        
        Object zz = request.getSession().getAttribute("user1");
        if(zz!=null){
        	//先检查用户是否处于登入状态
            request.getRequestDispatcher("check.do").forward(request,response);
			out.print("");  
        }else if(username!=null && md5oper!=null){//&& password!=null) {
            String[] arr = username.split(md5oper);
            if(arr.length==2){
	            username = arr[0];
	            password = arr[1];
            }
        	if(username!=null && password!=null) {
	            //Cookie中含有用户名和密码，提交跳转到用户验证页面
	        	//自动产生验证码
	            //request.removeAttribute("user");
	            //request.removeAttribute("pass");
	            request.setAttribute("user", username);
	            request.setAttribute("pass",password);
            	request.getSession().setAttribute("md5_oper",md5oper);
	            String validate_code;
	    		try {
	    			validate_code = DAOFactory.createValidate_code().makeValidate_code(4, 0,request.getSession());
	    		} catch (Exception e) {
	    			throw new NullPointerException("获取验证码出错");
	    		}
	            request.setAttribute("checkcode",validate_code);
	            request.getRequestDispatcher("check.do").forward(request,response);
				out.print("");  
        	}else 	//含有"userinfo"的cookie但未校验通过，所以需要清楚cookie
        		response.sendRedirect("logout.jsp"); 
        }else{
        	out.print("{success:false,errors:[]}");                  	
        }
    }

}
